package com.wsm.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsm.blog.domain.ContentDO;
import com.wsm.blog.mapper.ContentMapper;
import com.wsm.blog.service.ContentService;
import com.wsm.common.annotation.RedisCache;
import com.wsm.common.utils.BaiduLinkPostUtil;
import com.wsm.common.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, ContentDO> implements ContentService {
	@Autowired
	private ContentMapper bContentMapper;
	@Autowired
	BaiduLinkPostUtil baiduLinkPostUtil;

	@Override
	public ContentDO get(Long cid) {
		return bContentMapper.selectById(cid);
	}

	@Override
	@RedisCache(value = "blog")
	public List<ContentDO> list(Map<String, Object> map) {
		return bContentMapper.list(map);
	}

	@Override
	@RedisCache(value = "blog")
	public List<ContentDO> listNotHaveContent(Map<String, Object> map) {
		return bContentMapper.listNotHaveContent(map);
	}

	@Override
	@RedisCache(value = "blog")
	public int count(Map<String, Object> map){
		return bContentMapper.count(map);
	}

	@Override
	@RedisCache(value = "blog", flush = true)
	public boolean save(ContentDO bContent) {
		bContent.setCid(SnowFlake.nextId());
		if ((bContentMapper.orSave(bContent) > 0)) {
			if (bContent.getAllowPing() == 1) {
				String http = "http://www.wangshimin.top/blog/open/post/" + bContent.getCid().toString();
				baiduLinkPostUtil.Post(new String[]{http});
			}
			return true;
		}
		return false;
	}

	@Override
	@RedisCache(value = "blog", flush = true)
	public boolean update(ContentDO bContent) {
		if (bContentMapper.orUpdate(bContent) > 0) {
			return true;
		}
		return false;
	}

	@Override
	@RedisCache(value = "blog", flush = true)
	public int remove(Long cid){
		return bContentMapper.remove(cid);
	}

	@Override
	@RedisCache(value = "blog", flush = true)
	public int batchRemove(Long[] cids){
		return bContentMapper.batchRemove(cids);
	}
	
}
